import React, { useState, useEffect } from 'react';
import Switch from '@mui/material/Switch';

const SwitchCell = ({ onSwitchChange, ...params }) => {
  // const initialState = params.row.status === 'active'; // 假设params.row.enabled存储了Switch的初始状态
  const [switchState, setSwitchState] = useState(params.row.status === 'active');
  const handleChange = (event) => {
    console.log(`Switch state changed to ${event.target.checked}`);
    // 这里可以添加更多的处理逻辑
    const newState = event.target.checked;
    setSwitchState(newState); // 更新本地状态
    if (onSwitchChange) {
      onSwitchChange(event, params);
    }
  };

  const stopPropagation = (event) => {
    event.stopPropagation();
  };
  // 添加键盘事件处理程序
  const handleKeyDown = (event) => {
    stopPropagation(event);
    if (event.key === 'Enter' || event.key === ' ') {
      // onClick();
    }
  };
  return (
    <div onClick={stopPropagation} role="button" tabIndex={0} onKeyDown={handleKeyDown}>
      <Switch checked={switchState} onChange={handleChange} /> {/* 使用initialState控制Switch的状态 */}
    </div>
  );
};

export default SwitchCell;
